package com.ftd.utils;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * Created by apple on 2016/11/1.
 */
public class HibernateUtil {
    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private static SessionFactory sessionFactory = null;
    static {
        try{
            Configuration configuration = new Configuration().configure();
            sessionFactory = configuration.buildSessionFactory();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static Session getSession() throws HibernateException
    {
        Session session = (Session) threadLocal.get();
        if (session == null || !session.isOpen()){
            if (sessionFactory == null){
                rebuildSessionFactory();
            }
            session = (sessionFactory != null)?sessionFactory.openSession():null;
            threadLocal.set(session);
        }
        return session;
    }
    public static void  rebuildSessionFactory(){
        try {
            Configuration configuration = new Configuration().configure();
            sessionFactory = configuration.buildSessionFactory();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    public static SessionFactory getSessionFactory(){
        return sessionFactory;
    }
    public static void closeSession()throws HibernateException
    {
        Session session = (Session)threadLocal.get();
        threadLocal.set(null);
        if (session!=null)
        {
            session.close();
        }
    }
}
